package com.xuanwei.rabbit_consume.listen;

import com.alibaba.fastjson.JSON;
import com.xuanwei.rabbit_consume.handler.OrderHandler;
import com.xuanwei.xwcommon.consts.MqType;
import com.xuanwei.xwcommon.entity.dto.MessageDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * @Author jiangyong
 * @Description TODO
 * @Date 2019/7/25 17:44
 **/
@Component
@RabbitListener(queues = {MqType.ORDER})
@Slf4j
public class OrderListen {

    private OrderHandler orderHandler;

    public OrderListen(OrderHandler orderHandler) {
        this.orderHandler = orderHandler;
    }

    @RabbitHandler
    public void process(String msg) {
        log.info("receive msg:{}", msg);
        MessageDto dto = JSON.parseObject(msg, MessageDto.class);
        switch (dto.getType()) {
            case ORDER_CLOSE:
                log.info("execute order close>>>>>{}", dto.getData());
                orderHandler.orderClose(Integer.parseInt(dto.getData().toString()));
                break;
            case ORDER_RECEIVE:
                log.info("execute order receive>>>>>{}", dto.getData());
                orderHandler.receive(Integer.parseInt(dto.getData().toString()));
                break;
            case ORDER_SUCCESS:
                log.info("execute order success>>>>>{}", dto.getData());
                orderHandler.success(Integer.parseInt(dto.getData().toString()));
                break;
            case ORDER_PAY_SUCCESS:
                log.info("execute order pay success>>>>>{}", dto.getData());
                orderHandler.orderSuccess(Integer.parseInt(dto.getData().toString()));
                break;
            case ORDER_REFUND_SUCCESS:
                log.info("execute order refund success>>>>>{}", dto.getData());
                orderHandler.orderRefundSuccess(Integer.parseInt(dto.getData().toString()));
                break;
            case ORDER_NO_PAY:
                log.info("execute order not pay >>>>>{}", dto.getData());
                orderHandler.notPay(Integer.parseInt(dto.getData().toString()));
                break;
            default:
                log.warn("no  type");
        }
    }
}
